/**
 * Poker game. Hold'em Limit Version
 *
 *All cards are listed according to the links below.
 *for example: hK - king of hearts, sT - 10 of spades, dJ - Jack of diamonds, c4 - four of clubs
 *d - diamonds
 *s - spades
 *h - hearts
 *c - clubs
 */
//all cards by numbers
var car = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 , 17, 18, 19, 20, 21, 22, 23,
           24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
           45, 46, 47, 48, 49, 50, 51];
//links to the cards images 
var cardlinks =["images/cards/d/d2.jpg", "images/cards/d/d3.jpg", "images/cards/d/d4.jpg",
                "images/cards/d/d5.jpg", "images/cards/d/d6.jpg", "images/cards/d/d7.jpg",
                "images/cards/d/d8.jpg", "images/cards/d/d9.jpg", "images/cards/d/dT.jpg",
                "images/cards/d/dJ.jpg", "images/cards/d/dQ.jpg", "images/cards/d/dK.jpg",
                "images/cards/d/dA.jpg", "images/cards/s/s2.jpg", "images/cards/s/s3.jpg",
                "images/cards/s/s4.jpg", "images/cards/s/s5.jpg", "images/cards/s/s6.jpg",
                "images/cards/s/s7.jpg", "images/cards/s/s8.jpg", "images/cards/s/s9.jpg",
                "images/cards/s/sT.jpg", "images/cards/s/sJ.jpg", "images/cards/s/sQ.jpg",
                "images/cards/s/sK.jpg", "images/cards/s/sA.jpg", "images/cards/h/h2.jpg",
                "images/cards/h/h3.jpg", "images/cards/h/h4.jpg", "images/cards/h/h5.jpg",
                "images/cards/h/h6.jpg", "images/cards/h/h7.jpg", "images/cards/h/h8.jpg",
                "images/cards/h/h9.jpg", "images/cards/h/hT.jpg", "images/cards/h/hJ.jpg",
                "images/cards/h/hQ.jpg", "images/cards/h/hK.jpg", "images/cards/h/hA.jpg",
                "images/cards/c/c2.jpg", "images/cards/c/c3.jpg", "images/cards/c/c4.jpg",
                "images/cards/c/c5.jpg", "images/cards/c/c6.jpg", "images/cards/c/c7.jpg",
                "images/cards/c/c8.jpg", "images/cards/c/c9.jpg", "images/cards/c/cT.jpg",
                "images/cards/c/cJ.jpg", "images/cards/c/cQ.jpg", "images/cards/c/cK.jpg",
                "images/cards/c/cA.jpg"];
//player banks(money the've got at the start of the game)	
var banks = [100, 100, 100, 100, 100, 100, 100, 100, 100];
//bets that players make
var plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
//True if the player didn't fold the cards
var ingame = [1, 1, 1, 1, 1, 1, 1, 1, 1];
//Current dealer in the game
var dealer=0;
//Phase of the game(0 - Preflop, 1 - flop, 2 - turn, 3- river)
var move = 0;
var time = 1000;

var gamecounter=0;

var ingamen=9;
var counter=0;

var sb = 2;//small blind
var bb = 4;//big blind
var bank = 0;//bank
var bet = 4;//smallest bet
// Current player
var k = 0;

var anal = [0, 0, 0, 0, 0]; // anal[0], anal[1] 1st/2nd card low = 1, medium = 2, high = 3,
//anal[2] connection (0, 1, 2, or 3). anal[3] suited(1) or not (0)
//anal[4] pair(1) or not(0)

//All player cards, that have been dealed
var plcards = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
//Cards on the table
var flopcards = [0, 0, 0, 0, 0];

//When the document loads, poker starts immediately:
$(document).ready(function() {
	//Is activated when document loads
	hidebuttons();
	addyourname();//change name
	animatetable();
	dealer = (Math.floor(Math.random()*8 + 1));
	setTimeout('startgame()',500);
});
//THE GAME START
function startgame(){
	/*jQuery.fx.off = true; //Turn off animations*/
	//Randomly Choose the first dealer
	prepreflop();
	dealers(dealer);
	$("#bank").html(bank);
	gamecounter+=1;
	addlog(0,"GAME nr "+gamecounter+" started");
	preflop();
};
function preflop() {
	k+=3;
	setTimeout('plmove()',5000);
}
//MOVES
function flop(){
	counter=0;
	ingamen=ingame.sum();
	move = 1;
	bank += plbet.sum();
	$("#bank").html(bank);
	$("div.playerbet").css("opacity", "0");
	plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
	bet = 0;
	t_card1 = randomcard();
	t_card2 = randomcard();
	t_card3 = randomcard();
	flopcards[0]=t_card1;
	flopcards[1]=t_card2;
	flopcards[2]=t_card3;
	$("img#t_card1").attr("src", cardlinks[flopcards[0]]);
	$("img#t_card2").attr("src", cardlinks[flopcards[1]]);
	$("img#t_card3").attr("src", cardlinks[flopcards[2]]);
	dealtcards(1,3);
	k=dealer+1;
	addlog(0,"FLOP bank: "+bank);
	setTimeout('plmove()',3000);
}
function turn(){
	counter=0;
	ingamen=ingame.sum();
	move = 2;
	bank += plbet.sum();
	$("#bank").html(bank);
	$("div.playerbet").css("opacity", "0");
	plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
	bet = 0;
	bb = bb*2;
	t_card4 = randomcard();
	flopcards[3]=t_card4;
	$("img#t_card4").attr("src", cardlinks[flopcards[3]]);
	dealtcards(4,4);
	k=dealer+1;
	addlog(0,"TURN bank: "+bank);
	setTimeout('plmove()',3000);
}
function river(){
	counter=0;
	ingamen=ingame.sum();
	move = 3;
	bank += plbet.sum();
	$("#bank").html(bank);
	$("div.playerbet").css("opacity", "0");
	plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
	bet = 0;
	t_card5 = randomcard();
	flopcards[4]=t_card5;
	$("img#t_card5").attr("src", cardlinks[flopcards[4]]);
	k=dealer+1;
	dealtcards(5,5);
	addlog(0,"RIVER bank: "+bank);
	setTimeout('plmove()',3000);
}
//PLAY
function plmove(){
	//The AI of the computer
	if (k >= 10){
		k = k-9;
	}
	time=1000;
	if(ingame[k-1]==1){
		counter+=1;
		var id="#player"+k;
		activeplayer(id);
		$(id).animate({"opacity": "1"},50);
		if (k == 1){
			pl1move();
		}
	//PREFLOP
		if (move==0){
			analys(plcards[k*2-1], plcards[k*2-2]);
			result = anal.join("");
			if ((anal[0]==3) && (anal[1]==3)){			
				if (bet == bb*4){
					call(k);
				}
				else {
					raise(k);
				}
			}
			else if ((result == 22110) || (result == 32110) || (result == 22001) || (result == 23110)){
				if (bet == bb*4){
					call(k);
				}
				else {
					raise(k);
				}
			}
			else if ((result == 11001) || (result == 23310) || (result == 32310) || (result == 23310) || (result == 11110)|| (result == 22210) || (result == 32210)){
				call(k);
			}
			else {
				fold(k);
			}
			if($("#pl"+k+"_state").html()!="FOLD"){
				// notactiveplayer(id);
			}
		}
	//FLOP AND TURN
		else if (move==1 || move==2){
			//activeplayer(id);
			result = flopanalys(plcards[k*2-1], plcards[k*2-2]);
			if (result == 2){			
				if (bet == bb*4){
					call(k);
				}
				else {
					raise(k);
				}
			}
			else if (result == 1){
				call(k);
			}
			else if (result == 0){
				if (bet == 0){
					check(k);
				}
				else{
					fold(k);
				}
			}
		}
	//RIVER
		else if (move==3){
			//activeplayer(id);
			if (bet == bb*4){
				call(k);
			}
			else {
				raise(k);
			}
		}
		if(ingame[k-1]==1){
			notactiveplayer(id);
		}
	}
	//if the player has folded
	else{
		time=0;
	}
	//End game control
	endcheck();
}
function pl1move() {
	//Move of the player
	if (ingame.sum()==1){
		endgame();
	}
	else{
		if (plbet[0] == bet){
			$("#check").css("opacity", "1");
			$("#check").attr("onClick", "plcheck()");
			$("#raise").css("opacity", "1");
			$("#raise").attr("onClick", "plraise()");
		}
		if (plbet[0] < bet && plbet[0] != bb*4){
			$("#fold").css("opacity", "1");
			$("#fold").attr("onClick", "plfold()");
			$("#call").css("opacity", "1");
			$("#call").attr("onClick", "plcall()");
			$("#raise").css("opacity", "1");
			$("#raise").attr("onClick", "plraise()");
		}
		if (plbet[0] == bb*4){
			//if the bet is maksimum, goes on
			hidebuttons();
			return
		}		
		wait();
	}
}
//GAME END/RESTART
function endgame(){
	//count bank
	bank+=plbet.sum();
	$(".playerbet").css("opacity", "0");
	$("#bank").html(bank);
	//show the cards
	for (var i=0;i<ingame.length;i++){
		if (ingame[i]==1){
			$("img#player"+(i+1)+"_1").attr("src", cardlinks[plcards[2*i]]);
			$("img#player"+(i+1)+"_2").attr("src", cardlinks[plcards[2*i+1]]);
		}
	}
	var winner=0;
	if (ingame.sum()==1){
		for (var i=0;i<ingame.length;i++){
			if (ingame[i]==1){
				winner=i+1;
			}
		}
	}
	else{
		winner = findwinner();
	}
	//analyse cards here and show the winner(k)
	//winner=k;
	//hightlight the winner, give the prize
	activeplayer("#player"+winner);
	$("#player"+winner).css({"border": "3px yellow solid","opacity":"1"});
	$("#pl"+winner+"_state").html("WINNER");
	addlog(winner,"WINs the bank: "+bank);
	banks[winner-1]+=bank;  bank=0; k=winner;
	//bank = 0
	//return to startgame state
	$("#continue").css("opacity", "1");
	$("#continue").attr("onClick", "returntostartgamestate()");
	//setTimeout('returntostartgamestate()',10000);
}
function returntostartgamestate(){
	$(".players").css("opacity", "0.5");
	$("#bank").html(bank);
	$("#pl"+k+"_bank").html(banks[k-1]);
	hidebuttons();
	//return player cards, delete status, winner border, hide player bets
	for (var i=0;i<ingame.length;i++){
		$("div#pl_"+(i+1)+"_bet").css("opacity", "0");
		$("#pl"+(i+1)+"_state").html("");
		$("#player"+(i+1)).css({"border": "3px grey groove"});
		$("#player"+(i+1)+"_1").attr("src", "images/card_back.jpg");
		$("#player"+(i+1)+"_2").attr("src", "images/card_back.jpg");
		if ($("#player"+(i+1)).css("height")=="46px"){
			notactiveplayer("#player"+(i+1));
		}
		if (ingame[i]==1){
			returnacard("#player"+(i+1)+"_1");
			returnacard("#player"+(i+1)+"_2");
		}
	}
	//return table cards
	for (var i=1;i<6;i++){
		returnacard("#t_card"+i);
	}
	//change dealer
	$("#dealer"+dealer).css("opacity", "0");
	if (dealer==9){
		dealer=1;
	}
	else{
		dealer+=1;
	}
	//All variables but banks to their initial state
	plbet = [0, 0, 0, 0, 0, 0, 0, 0, 0];
	ingame = [1, 1, 1, 1, 1, 1, 1, 1, 1];
	move = 0;
	t = 9;
	counter = 0;
	sb = 2;
	bb = 4;
	bank = 0;
	bet = 4;
	car = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 , 17, 18, 19, 20, 21, 22, 23,
           24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
           45, 46, 47, 48, 49, 50, 51];
	//Start the next game
	setTimeout('startgame()',3000);
}
//COMPUTER ACTIONS
function check(k){
	banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
	plbet[k-1] = bet;//player's bet
	$("#pl"+k+"_bank").html(banks[k-1]);
	$("div#pl_"+k+"_bet").html(plbet[k-1]);
	$("div#pl_"+k+"_bet").css("opacity", "1");
	addlog(k,"Checks");
}
function fold(k){
	$("#pl"+k+"_state").html("FOLD");
	returnacard("#player"+k+"_1");
	returnacard("#player"+k+"_2");
	ingame[k-1]=0;
	addlog(k,"Folds");
}
function call(k){
	banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
	plbet[k-1] = bet;//player's bet
	$("#pl"+k+"_bank").html(banks[k-1]);
	$("div#pl_"+k+"_bet").html(plbet[k-1]);
	$("div#pl_"+k+"_bet").css("opacity", "1");
	addlog(k,"Calls");
}
function raise(k){
	bet = bet + bb; // bet raises per 1 bb	
	banks[k-1] = banks[k-1] - bet + plbet[k-1];// player's bank
	plbet[k-1] = bet;	//player's bet
	$("#pl"+k+"_bank").html(banks[k-1]);
	$("div#pl_"+k+"_bet").html(plbet[k-1]);
	$("div#pl_"+k+"_bet").css("opacity", "1");
	addlog(k,"Raises");
}
//PLAYER ACTIONS
function plfold(){
	//Drop cards
	$("#pl1_state").html("FOLD");
	returnacard("#player1_1");
	returnacard("#player1_2");
	hidebuttons();
	ingame[0]=0;
	addlog(1,"Folds");
	endcheck();
}
function plraise(){

	//Raise players bet
	if (bet == bb*4){
		plcall();
	}
	else {
		bet = bet + bb; // bet raises per 1 bb	
		banks[0] = banks[0] - bet + plbet[0];// player's bank
		plbet[0] = bet;	//player's bet
		$("#pl1_bank").html(banks[0]);
		$("div#pl_1_bet").html(plbet[0]);
		$("div#pl_1_bet").css("opacity", "1");	
	}
	notactiveplayer("#player1");
	hidebuttons();
	//notactiveplayer("#player1");
	addlog(1,"Raises");
	endcheck();
}
function plcall(){
	//Same bet as the other players
	banks[0] = banks[0] - bet + plbet[0];// player's bank
	plbet[0] = bet;	//player's bet
	$("#pl1_bank").html(banks[0]);
	$("div#pl_1_bet").html(plbet[0]);
	$("div#pl_1_bet").css("opacity", "1");

	hidebuttons();
	notactiveplayer("#player1");
	addlog(1,"Calls");
	endcheck();
}
function plcheck(){
	//Do nothing
	hidebuttons();
	notactiveplayer("#player1");
	addlog(1,"Checks");
	endcheck();
}
//FUNCTIONS
function prepreflop(){
	//Deals the player cards
	for (var i=0;i<plcards.length;i++){
		plcards[i]=randomcard();
	}
	$("#player1_1").attr("src", cardlinks[plcards[0]]);
	$("#player1_2").attr("src", cardlinks[plcards[1]]);
	dealcards(1,1);
	hidebuttons();

	//for debugging, show computer player cards 

	/*$("img#player2_1").attr("src", cardlinks[plcards[2]]);
	$("img#player2_2").attr("src", cardlinks[plcards[3]]);
	$("img#player3_1").attr("src", cardlinks[plcards[4]]);
	$("img#player3_2").attr("src", cardlinks[plcards[5]]);
	$("img#player4_1").attr("src", cardlinks[plcards[6]]);
	$("img#player4_2").attr("src", cardlinks[plcards[7]]);
	$("img#player5_1").attr("src", cardlinks[plcards[8]]);
	$("img#player5_2").attr("src", cardlinks[plcards[9]]);
	$("img#player6_1").attr("src", cardlinks[plcards[10]]);
	$("img#player6_2").attr("src", cardlinks[plcards[11]]);
	$("img#player7_1").attr("src", cardlinks[plcards[12]]);
	$("img#player7_2").attr("src", cardlinks[plcards[13]]);
	$("img#player8_1").attr("src", cardlinks[plcards[14]]);
	$("img#player8_2").attr("src", cardlinks[plcards[15]]);
	$("img#player9_1").attr("src", cardlinks[plcards[16]]);
	$("img#player9_2").attr("src", cardlinks[plcards[17]]);//*/
}
function check(){

	//check if all active players have same bets, if round complete - next turn
	var a=0;
	var b=0;
	for (i=0; i < ingame.length; i++){
		if (ingame[i]==1 && plbet[i]==bet){
			a+=1;
		}
		if (ingame[i]==1){
			b+=1;
		}
	}
	if (a == b){
		return 1;	
	}
	else {
		return 0;
	}
}
function endcheck(){
	$("#player"+k).animate({"opacity": "0.5"},1000);
	if (ingame.sum()==1)setTimeout('endgame()',time);
	else if (check() == 1 && move==0 && counter>=ingamen)setTimeout('flop()',time);
	else if (check() == 1 && move==1 && counter>=ingamen)setTimeout('turn()',time);
	else if (check() == 1 && move==2 && counter>=ingamen)setTimeout('river()',time);
	else if (check() == 1 && move==3 && counter>=ingamen)setTimeout('endgame()',time);
	else{
		k+=1;
		setTimeout('plmove()',time);
	}
}
function randomcard(){
	//chooses a random card from an array and deletes it from the array
	var a = (Math.floor(Math.random()*car.length));
	b = car[a];
	car.splice(a, 1);
	return b;
}
Array.prototype.sum = function(){
	for(var i=0,sum=0;i<this.length;sum+=this[i++]);
	return sum;
};
function hidebuttons(){
	//Hide all buttons from the player, so that he cannot affect the game while it is not his turn
	$("#continue").css("opacity", "0");
	$("#continue").attr("onClick", "");
	$("#bet").css("opacity", "0");
	$("#bet").attr("onClick", "");
	$("#fold").css("opacity", "0");
	$("#fold").attr("onClick", "");
	$("#call").css("opacity", "0");
	$("#call").attr("onClick", "");
	$("#raise").css("opacity", "0");
	$("#raise").attr("onClick", "");
	$("#check").css("opacity", "0");
	$("#check").attr("onClick", "");
}
function dealers(a) {
	//Sets a dealer and compulsory bets (small blind and big blind)
	$("img#dealer"+a).css("opacity", "1");//Shows, who is the dealer
	dealer=a;
	k=a;
	if (a==9){
		a=0;
	}
	banks[a] = banks[a]-sb;
	plbet[a] = plbet[a] + sb;
	$("div#pl_"+(a+1)+"_bet").html(plbet[a]);//
	$("#pl"+(a+1)+"_bank").html(banks[a]);
	$("div#pl_"+(a+1)+"_bet").css("opacity", "1");
	if (a==8){
		a=-1;
	}
	$("div#pl_"+(a+2)+"_bet").html(bb);
	$("#pl"+(a+2)+"_bank").html(banks[a+1]-bb);
	banks[a+1] = banks[a+1]-bb;
	plbet[a+1] = plbet[a+1] + bb;
	$("div#pl_"+(a+2)+"_bet").css("opacity", "1");
}
//ANALYSIS
function sortNumber(a,b)
{
	return a - b;
}
function analys(card1, card2){
	//Card analysys of the computer players
	if ((card1%13 <= 4) && (card1%13 >= 0)){
		anal[0] = 1;
	}
	if ((card2%13 <= 4) && (card2%13 >= 0)){
		anal[1] = 1;
	}

	if ((card1%13 <= 9) && (card1%13 >= 5)){
		anal[0] = 2;
	}
	if ((card2%13 <= 9) && (card2%13 >= 5)){
		anal[1] = 2;
	}

	if ((card1%13 <= 12) && (card1%13 >= 10)){
		anal[0] = 3;
	}
	if ((card2%13 <= 12) && (card2%13 >= 10)){
		anal[1] = 3;
	}

	if (card1%13 == card2%13){
		anal[4]=1;
		anal[2]=0;
		anal[3]=0;
	}
	else{
		if (card1<=12 && card2 <=12){
			anal[3]=1;
		}
		else if ((card1<=25 && card1>=13) && (card2 <=25 && card2>=13)){
			anal[3]=1;
		}
		else if ((card1<=38 && card1>=26) && (card2 <=38 && card2>=26)){
			anal[3]=1;
		}
		else if ((card1<=51 && card1>=39) && (card2 <=51 && card2>=39)){
			anal[3]=1;
		}
		else{
			anal[3]=0;
		}
		if (Math.abs(card1%13-card2%13)==1){
			anal[2]=1;
		}
		else if (Math.abs(card1%13-card2%13)==2){
			anal[2]=2;
		}
		else if (Math.abs(card1%13-card2%13)==3){
			anal[2]=3;
		}
		else{
			anal[2]=0;
		}
	}
}
function flopanalys(card1, card2){
	var cards = [0, 0, 0, 0, 0];
	cards[0] = flopcards[0]%13;
	cards[1] = flopcards[1]%13;
	cards[2] = flopcards[2]%13;
	cards[3] = card1%13;
	cards[4] = card2%13;

	cards = cards.sort(sortNumber);
	//trips or 4th
	if (((cards[0]==cards[1])&&(cards[1]==cards[2])) || ((cards[1]==cards[2])&&(cards[2]==cards[3])) || ((cards[2]==cards[3])&&(cards[3]==cards[4]))){
		return 2;//raise
	}
	// straight or straight flush or royal flush
	else if ((cards[4]-cards[0])== 4){
		return 2; 
	}
	//flush
	else if (card1<=12 && card2 <=12 && flopcards[0]<=12 && flopcards[1]<=12 && flopcards[2]<=12){
		return 2;
	}
	else if ((card1<=25 && card2<=25 && flopcards[0]<=25 && flopcards[1]<=25 && flopcards[2]<=25) && (card1>=12 && card2 >=12 && flopcards[0]>=12 && flopcards[1]>=12 && flopcards[2]>=12)){
		return 2;
	}
	else if ((card1>=26 && card2>=26 && flopcards[0]>=26 && flopcards[1]>=26 && flopcards[2]>=26) && (card1<=38 && card2 <=38 && flopcards[0]<=38 && flopcards[1]<=38 && flopcards[2]<=38)){
		return 2;
	}
	else if ((card1>=39 && card2>=39 && flopcards[0]>=39 && flopcards[1]>=39 && flopcards[2]>=39) && (card1<=51 && card2 <=51 && flopcards[0]<=51 && flopcards[1]<=51 && flopcards[2]<=51)){
		return 2;
	}
	//top pair or over pair or 2 pair
	else if ((card1%13==card2%13) || (card1%13==flopcards[2]%13) || (card2%13==flopcards[2]%13)){
		return 2;//call
	}
	//straight dro (2)
	else if (((cards[4]-cards[1])==3) && ((cards[3]-cards[0])==3)){
		return 1;// call
	}
	else{
		return 0;// fold
	}
}

//returns combination. a - array of 5 cards
function combanalys(a){
	l=[];
	b = [a[0], a[1], a[2], a[3], a[4]];
	b = b.sort(sortNumber);// b sorted array without %13 sortNumber
	for (i=0; i<a.length; i++){
		a[i]=a[i]%13;
	}
	a = a.sort(sortNumber); // a sorted array with %13 sortNumber
	
	
	
	//Royal flush
	if ((b[4]-b[0]==4)&&(b[4]%13==12)){
		l = [9,0];
		return l.join("");
	}
	//Straight flush
	else if (b[4]-b[0]==4){
		l = [8,b[4]%13];
		return l.join("");
	}
	//4th
	else if ((a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3])||(a[1]==a[2]&&a[2]==a[3]&&a[3]==a[4])){
		l = [7, a[2]];
		return l.join("");
	}
	// full house
	else if ((a[0]==a[1]&&a[2]==a[3]&&a[3]==a[4])){
		l =[6,a[4],a[1]];
		return l.join("");
	}
	else if ((a[0]==a[1]&&a[1]==a[2]&&a[3]==a[4])){
		l = [6, a[1], a[4]];
		return l.join("");
	}
	//flush
	else if (((b[4]<13))||((b[0]>12)&&(b[4]<25))||((b[0]>24)&&(b[4]<37))||(b[0]>36)){
		l = [5,b[4]];
		return l.join("");
	}
	//Straight
	else if(a[4]-a[0]==4){
		l = [4, a[4]];
		return l.join("");
	}
	//Trips
	else if ((a[0]==a[1]&&a[1]==a[2])||(a[1]==a[2]&&a[2]==a[3])||(a[2]==a[3]&&a[3]==a[4])){
		l = [3, a[2]];
		return l.join("");
	}
	//2 Pair
	else if ((a[0]==a[1]&&a[2]==a[3])||(a[0]==a[1]&&a[3]==a[4])||(a[1]==a[2]&&a[3]==a[4])){
		l = [2, a[3], a[1]];
		return l.join("");
	}
	// Pair 
	else if (a[4]==a[3]){
		l = [1, a[4], a[2]];
		return l.join("");
	}
	else if (a[2]==a[3]){
		l = [1, a[3], a[4]];
		return l.join("");
	}
	else if (a[1]==a[2]){
		l = [1, a[2], a[4]];
		return l.join("");
	}
	else if (a[0]==a[1]){
		l = [1, a[1], a[4]];
		return l.join("");
	}
	// High card
	else{
		l = [0,a[4]];
		return l.join("");
	}
}
//returns highest compination af player
function playcomb(player){
	c1 = plcards[player*2-1];
	c2 = plcards[player*2-2];
	c3 = flopcards[0];
	c4 = flopcards[1];
	c5 = flopcards[2];
	c6 = flopcards[3];
	c7 = flopcards[4];
	
	array = [combanalys([c1, c2, c3, c4, c5]), combanalys([c1, c2, c3, c4, c7]),
	         combanalys([c1, c2, c3, c4, c6]), combanalys([c1, c2, c3, c6, c7]),
	         combanalys([c1, c2, c3, c5, c7]), combanalys([c1, c2, c3, c5, c6]),
	         combanalys([c1, c2, c5, c6, c7]), combanalys([c1, c2, c4, c6, c7]),
	         combanalys([c1, c2, c4, c5, c7]), combanalys([c1, c2, c4, c5, c6]),
	         combanalys([c1, c4, c5, c6, c7]), combanalys([c1, c3, c5, c6, c7]),
	         combanalys([c1, c3, c4, c6, c7]), combanalys([c1, c3, c4, c5, c7]),
	         combanalys([c1, c3, c4, c5, c6]), combanalys([c3, c4, c5, c6, c7]),
	         combanalys([c2, c4, c5, c6, c7]), combanalys([c2, c3, c5, c6, c7]),
	         combanalys([c2, c3, c4, c6, c7]), combanalys([c2, c3, c4, c5, c7]),
	         combanalys([c2, c3, c4, c5, c6]),];
	array = array.sort();
	return array[20];
}

//finding player with highest combination
function findwinner(){
    arr = [0, 0, 0, 0, 0, 0, 0, 0, 0];
	arr[0] = playcomb(1);
    arr[1] = playcomb(2);
    arr[2] = playcomb(3);
    arr[3] = playcomb(4);
    arr[4] = playcomb(5);
    arr[5] = playcomb(6);
    arr[6] = playcomb(7);
    arr[7] = playcomb(8);
    arr[8] = playcomb(9);
    for (i=0; i<9; i++){
    	if (ingame[i]==0){
    		arr[i]=0;
    	}
    }
    arr2 = [arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], arr[6], arr[7], arr[8]];
    arr2 = arr2.sort();
    
    for (i=0; i<arr.length; i++){
            if (arr2[arr2.length-1] == arr[i]){
            		ut = i+1;
            }
    }
    return ut;
}